package com.lyhlmj.oop_client.Style.TextFields;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;

//显示消息列表的控件
public class MsgBox extends JTextPane {
    private List<Item> itemList = new ArrayList<>();  //存放消息的列表
    private boolean flag = true;

    public MsgBox() {
        initDocStyle(this.getStyledDocument());  //页面初始化，对字体进行设置

        this.setEditable(false);  //可编辑
    }

    // 添加几个样式 name, time, content
    private void initDocStyle(StyledDocument doc) {
        Style def = StyleContext.getDefaultStyleContext().
                getStyle(StyleContext.DEFAULT_STYLE);


        if (true) {  //用户名字体设置
            Style s = doc.addStyle("username", def);
            StyleConstants.setFontFamily(s, "隶书");
            StyleConstants.setBold(s, true);
            StyleConstants.setFontSize(s, 20);
        }
        if (true) {  //消息发送时间字体设置
            Style s = doc.addStyle("time", def);
            StyleConstants.setFontFamily(s, "隶书");
            StyleConstants.setBold(s, true);
            StyleConstants.setFontSize(s, 20);
        }

        if (true) {  //消息内容字体设置
            Style s = doc.addStyle("content", def);
            StyleConstants.setFontFamily(s, "隶书");
            StyleConstants.setBold(s, false);
            StyleConstants.setFontSize(s, 20);
        }
    }

    // 添加一条消息
    public void appendMessage(JSONObject msg) {
//        从msg中取值
        String name = msg.optString("from", "");
        String time = msg.optString("time", "");
        String content = msg.optString("content", "");

        StyledDocument doc = getStyledDocument();

        try {
            Item item = new Item();

            String text = name;
            doc.insertString(doc.getLength(), text, doc.getStyle("username"));
            item.length += text.length();

            text = " " + time + "\n";
            doc.insertString(doc.getLength(), text, doc.getStyle("time"));
            item.length += text.length();

            text = " " + content + "\n\n";
            doc.insertString(doc.getLength(), text, doc.getStyle("content"));
            item.length += text.length();

            itemList.add(item);

        } catch (BadLocationException e) {
            e.printStackTrace();
        }

        // 当消息太多时，删除最前面的消息
        if (itemList.size() > 40) {
            Item item = itemList.get(0);
            itemList.remove(0);

            try {
                doc.remove(0, item.length);
            } catch (BadLocationException e) {
                e.printStackTrace();
            }
        }
    }

    // 记录每一段消息的位置，方便清理删除
    private static class Item {
        int length;
    }

    public void clear() {
        itemList.clear();
        try {
            this.getStyledDocument().remove(0, this.getStyledDocument().getLength());
        } catch (BadLocationException e) {
            e.printStackTrace();
        }
    }
}
